package com.spukmk2me.spukmk2mesceneeditor.gui;

import java.awt.Frame;
import javax.swing.JDialog;
import javax.swing.ListSelectionModel;
import javax.swing.DefaultListModel;

import com.spukmk2me.video.IVideoDriver;
import com.spukmk2me.resource.IResource;
import com.spukmk2me.resource.ResourceSet;

public final class ImageResourceDialog extends JDialog
{
    public ImageResourceDialog( ResourceSet resourceSet, IVideoDriver vdriver,
        Frame parent, boolean modal )
    {
        super( parent, modal );
        initComponents();
        m_resourceSet   = resourceSet;
        m_vdriver       = vdriver;
        ReloadData();
    }

    private void ReloadData()
    {
        m_resourceList.getModel();

        int nResource = m_resourceSet.GetNumberOfResources(
            IResource.RT_IMAGERESOURCE );
        DefaultListModel model = new DefaultListModel();

        for ( int i = 0; i != nResource; ++i )
        {
            model.addElement( m_resourceSet.GetResource(
                i, IResource.RT_IMAGERESOURCE ).GetProxyName() );
        }

        m_resourceList.setModel( model );
        m_resourceList.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        m_addButton = new javax.swing.JButton();
        m_removeButton = new javax.swing.JButton();
        m_closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Image resource");
        setAlwaysOnTop(true);
        setModal(true);
        setResizable(false);

        jLabel1.setText("Image resources:");

        m_resourceList.setModel(
            new javax.swing.DefaultListModel()
        );
        jScrollPane1.setViewportView(m_resourceList);

        m_addButton.setText("Add");
        m_addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_addButtonActionPerformed(evt);
            }
        });

        m_removeButton.setText("Remove");
        m_removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_removeButtonActionPerformed(evt);
            }
        });

        m_closeButton.setText("Close");
        m_closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                m_closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 247, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(m_addButton, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                            .addComponent(m_removeButton, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE)
                            .addComponent(m_closeButton, javax.swing.GroupLayout.DEFAULT_SIZE, 90, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(m_addButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(m_removeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                        .addComponent(m_closeButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void m_closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_closeButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_m_closeButtonActionPerformed

    private void m_addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_addButtonActionPerformed

        AddImageResourceDialog addDialog = new AddImageResourceDialog(
            m_resourceSet, m_vdriver, this, true );

        int width   = addDialog.getWidth();
        int height  = addDialog.getHeight();

        /*addDialog.setBounds(
            (this.getOwner().getWidth() - width >> 1) + this.getOwner().getX(),
            (this.getOwner().getHeight() - height >> 1) +
                this.getOwner().getY(),
            width, height );*/
        addDialog.setVisible( true );
        ReloadData();
    }//GEN-LAST:event_m_addButtonActionPerformed

    private void m_removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_m_removeButtonActionPerformed

        int index = m_resourceList.getSelectedIndex();

        if ( index != -1 )
        {
            DefaultListModel model =
                (DefaultListModel)m_resourceList.getModel();

            m_resourceSet.RemoveResource(
                m_resourceSet.GetResource( index, IResource.RT_IMAGERESOURCE ).
                    GetProxyName(),
                IResource.RT_IMAGERESOURCE );
            model.remove( index );
        }
    }//GEN-LAST:event_m_removeButtonActionPerformed

    private ResourceSet m_resourceSet;
    private IVideoDriver    m_vdriver;

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton m_addButton;
    private javax.swing.JButton m_closeButton;
    private javax.swing.JButton m_removeButton;
    private final javax.swing.JList m_resourceList = new javax.swing.JList();
    // End of variables declaration//GEN-END:variables
}
